package com.fire.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fire.bo.classIndex.req.ClassIndexReq;
import com.fire.bo.classIndex.req.GoodsClassReq;
import com.fire.bo.classIndex.req.GoodsReq;
import com.fire.bo.classIndex.req.SelClassIndexReq;
import com.fire.bo.classIndex.res.ClassGoodRes;
import com.fire.bo.classIndex.res.ClassIndexRes;
import com.fire.bo.coupon.req.DelCouponReq;
import com.fire.dao.ClassIndexDao;
import com.fire.service.ClassIndexService;

@Service
public class ClassIndexServiceImpl implements ClassIndexService
{

	@Autowired
	private ClassIndexDao classIndexDao;
	
	@Override
	public void addClassIndex(ClassIndexReq bean) throws SQLException
	{
		classIndexDao.addClassIndex(bean);
	}

	@Override
	public void updateClassIndex(ClassIndexRes bean) throws SQLException
	{
		classIndexDao.updateClassIndex(bean);
	}

	@Override
	public void delClassIndex(DelCouponReq bean) throws SQLException
	{
		classIndexDao.delClassIndex(bean);
	}

	@Override
	public List<ClassIndexRes> selClassIndex(SelClassIndexReq bean)
			throws SQLException
	{
		return classIndexDao.selClassIndex(bean);
	}

	@Override
	public ClassIndexRes selClassIndexById(DelCouponReq bean)
			throws SQLException
	{
		return classIndexDao.selClassIndexById(bean);
	}

	@Override
	public List<ClassGoodRes> findAllGood(GoodsReq bean) throws SQLException {
		int page = bean.getPage();
		bean.setPage((page-1) * bean.getPageSize());
		List<ClassGoodRes> list = classIndexDao.findAllGood(bean);
		
		for (ClassGoodRes classGoodRes : list) {
			String id = classGoodRes.getId();
			String str ="";
			Map<String, Object> map = new HashMap<>();
			map.put("goodId", id);
			List<Map<String, Object>> listGood = classIndexDao.selClassByGoodId(map);
			for (Map<String, Object> info : listGood) {
				str+=info.get("className") + ",";
			}
			
			if(listGood.size() != 0) {
				classGoodRes.setClassName(str.substring(0, str.length() -1));
			}
		}
		return list;
	}

	@Override
	public List<ClassIndexRes> findAllClass(String classId) throws SQLException {
		Map<String, Object> map = new HashMap<>();
		map.put("classId", classId);
		
		return classIndexDao.findAllClass(map);
	}

	@Override
	@Transactional
	public void insertClassGood(List<GoodsClassReq> info) throws SQLException {
		
		String goodId = "";
		
		if(info.size() != 0) {
			goodId = info.get(0).getProductId();
			Map<String, Object> map = new HashMap<>();
			map.put("goodId", goodId);
			classIndexDao.delClassGood(map);
		}
		
		for (GoodsClassReq goodsClassReq : info) {
			classIndexDao.insertClassGood(goodsClassReq);
		}
	}

	

	@Override
	public int findAllGoodCount(GoodsReq bean) throws SQLException {
		return classIndexDao.findAllGoodCount(bean);
	}

	@Override
	public List<ClassIndexRes> findAllClass(Map<String, Object> map) throws SQLException {
		return classIndexDao.findAllClass(map);
	}

}
